package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.JobResponsibility;
import com.amsp.oa.oademo.mapper.JobResponsibilityMapper;
import com.amsp.oa.oademo.service.JobResponsibilityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class JobResponsibilityServiceImpl extends ServiceImpl<JobResponsibilityMapper, JobResponsibility>
        implements JobResponsibilityService {

    @Override
    public List<JobResponsibility> getList(Integer page, Integer limit, String name, String department) {
        LambdaQueryWrapper<JobResponsibility> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.like(JobResponsibility::getName, name);
        }
        if (StringUtils.hasText(department)) {
            wrapper.like(JobResponsibility::getDepartment, department);
        }
        Page<JobResponsibility> pageParam = new Page<>(page, limit);
        return this.page(pageParam, wrapper).getRecords();
    }

    @Override
    public long getTotal(String name, String department) {
        LambdaQueryWrapper<JobResponsibility> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(name)) {
            wrapper.like(JobResponsibility::getName, name);
        }
        if (StringUtils.hasText(department)) {
            wrapper.like(JobResponsibility::getDepartment, department);
        }
        return this.count(wrapper);
    }

    @Override
    public JobResponsibility create(JobResponsibility jobResponsibility) {
        this.save(jobResponsibility);
        return jobResponsibility;
    }

    @Override
    public JobResponsibility update(Long id, JobResponsibility jobResponsibility) {
        jobResponsibility.setId(id);
        this.updateById(jobResponsibility);
        return jobResponsibility;
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

    //@Override
    //public JobResponsibility getById(Long id) {
    //    return this.getById(id);
    //}
}